package com.jasmine.bjstkserver.server.folder;

import com.jasmine.auth.util.AuthUtil;
import com.jasmine.bjstkserver.server.folder.model.FolderEntity;
import com.jasmine.bjstkserver.server.folder.model.FolderReq;
import com.jasmine.core.dto.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author jasmineXz
 */
@RestController
@RequestMapping("/folder")
public class FolderController {

    @Autowired
    private FolderService folderService;

    /**
     * 树状列表
     * @param folder 文件夹类型/用户ID,会根据该字段缓存
     */
    @GetMapping("/tree")
    public R trees(@ModelAttribute FolderReq folder) {
        folder.setUserId(AuthUtil.getUserId());
        return R.ok(folderService.findTree(folder));
    }

    /**
     * 新增文件
     * @param folder 文件夹参数
     */
    @PostMapping
    public R insert (@RequestBody FolderEntity folder) {
        folder.setUserId(AuthUtil.getUserId());
        folderService.insert(folder);
        return R.ok();
    }

    /**
     * 修改文件夹
     * @param folder 文件夹信息
     */
    @PutMapping
    public R update (@RequestBody FolderEntity folder) {
        folder.setUserId(AuthUtil.getUserId());
        folderService.updateOne(folder);
        return R.ok();
    }

    /**
     * 删除文件夹,会遍历删除子文件夹,文件夹下的文件会移动至默认文件夹
     * @param folder 文件夹信息
     */
    @DeleteMapping
    public R delete (@RequestBody FolderReq folder) throws Exception {
        folder.setUserId(AuthUtil.getUserId());
        folderService.deleteOne(folder);
        return R.ok();
    }
}
